package com.vietnam.power.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.domain.Product;
import com.vietnam.power.domain.ProductSearchCriteria;
import com.vietnam.power.service.AppConfigurationService;
import com.vietnam.power.service.ProductService;

@Controller
public class ProductController {
	private static Logger logger = Logger.getLogger(ProductController.class);
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private ProductService productService;
	
	@Autowired
	private AppConfigurationService appConfigurationService;

	@RequestMapping(value = { "/searchProduct" }, method = RequestMethod.GET)
	public ModelAndView searchProduct(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, value = "productGroupId") String productGroupId,
			@RequestParam(required = false, value = "page") String page) {
		ModelAndView mav = new ModelAndView("client/products");
		int currentPage = 1;
		logger.debug("productGroupId: " + productGroupId + "page " + page);
		if (StringUtils.isNotBlank(page)) {
			currentPage = Integer.parseInt(page);
		}

		if (StringUtils.isBlank(productGroupId)) {
			mav.addObject("productList", productService.searchProductByCriteria(null, currentPage));
		} else {
			ProductSearchCriteria psc = new ProductSearchCriteria();
			psc.setProductGroupId(Integer.valueOf(productGroupId));
			mav.addObject("productList", productService.searchProductByCriteria(psc, currentPage));
			mav.addObject("productGroupId", productGroupId);
		}
		
		mav.addObject("pageTitle", messageSource.getMessage("top.header.product", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchProductByKeyword" }, method = RequestMethod.POST)
	public ModelAndView searchProductByKeyword(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, value = "keyword") String keyword,
			@RequestParam(required = false, value = "page") String page) {
		ModelAndView mav = new ModelAndView("client/products");

		int currentPage = 1;
		if (StringUtils.isNotBlank(page)) {
			currentPage = Integer.parseInt(page);
		}
		ProductSearchCriteria psc = new ProductSearchCriteria();
		psc.setKeyword(keyword);
		psc.setName(keyword);
		
		mav.addObject("productList", productService.searchProductByCriteria(psc, currentPage));
		mav.addObject("keyword", keyword);
		mav.addObject("pageTitle", messageSource.getMessage("top.header.product", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/viewProduct" }, method = RequestMethod.GET)
	public ModelAndView viewProduct(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, value = "pId") int productId,
			@RequestParam(required = false, value = "page") String page) {
		ModelAndView mav = new ModelAndView("client/viewProduct");
		int currentPage = 1;
		logger.debug("productGroupId: " + productId + "page " + page);
		if (StringUtils.isNotBlank(page)) {
			currentPage = Integer.parseInt(page);
		}
		Product p = productService.getProductById(productId);

		ProductSearchCriteria psc = new ProductSearchCriteria();
		psc.setProductGroupId(p.getProductGroupId());
		psc.setCurrentProductId(productId);

		List<Product> productRelatedList = productService.searchProductByCriteria(psc, currentPage);

		mav.addObject("currentProduct", p);
		mav.addObject("productList", productRelatedList);
		mav.addObject("pageTitle", messageSource.getMessage("top.header.product", null, LocaleContextHolder.getLocale()));
		return mav;
	}

}
